package com.th.supcom.pds.drugsupply.server;

import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.th.supcom.pds.domain.pojo.PhamBasicInfo;
import com.th.supcom.pds.domain.vo.PdsPackageInfoVO;
import com.th.supcom.pds.drugsupply.client.model.PdsPackageInfoModel;
import com.th.supcom.pds.drugsupply.client.service.PdsPackageMaintainRpc;
import com.th.supcom.pds.internal.api.PdsSupplyPackageService;

/**
 * PhamBasicInfo 控制层.
 * 
 * 药品信息.
 */
public class PdsSupplyPackageServiceController extends GwtController implements PdsPackageMaintainRpc
{

    /** 日志记录器. */
    private static final Logger s_logger = LoggerFactory.getLogger (PhamBasicInfo.class);

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    /**
     * add by Zli
     */
    private static final long serialVersionUID = -7252559535751128820L;

    private PdsSupplyPackageService m_pdsSupplyPackageService;

    /**
     * 默认构造函数.
     */
    public PdsSupplyPackageServiceController ()
    {

    }

    @Override
    public void deletePackageInfo (final List <Long> serialNolist) throws UiException
    {
        PdsSupplyPackageServiceController.s_logger.debug ("进入deletePackageInfo方法,入参:");
        if (serialNolist.isEmpty ())
        {
            throw new UiException ("删除数据为空");
        }
        try
        {
            m_pdsSupplyPackageService.removePdsPackageInfoByList (serialNolist);
        }
        catch (final Exception e)
        {
            PdsSupplyPackageServiceController.s_logger.error (e.getMessage ());
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public List <PdsPackageInfoModel> getPackageInfoByName (final String name) throws UiException
    {
        PdsSupplyPackageServiceController.s_logger.debug ("进入getPackageInfoByName方法,入参:");
        List <PdsPackageInfoVO> volist = null;
        int count = 0;
        try
        {
            count = m_pdsSupplyPackageService.findPdsPackageInfoVOCountByName (name);
            volist = m_pdsSupplyPackageService.findPdsPackageInfoVOWithPageByName (0, count, name);

        }
        catch (final Exception e)
        {
            PdsSupplyPackageServiceController.s_logger.error (e.getMessage ());
            e.printStackTrace ();
            throw new UiException ("查询失败", e);
        }
        final List <PdsPackageInfoModel> drugInfoModelList = ConvertUtils.convert (PdsPackageInfoModel.class, volist);
        return drugInfoModelList;

    }

    @Override
    public PagingLoadResult <PdsPackageInfoModel> getPackageInfoListWithPaging (final PagingLoadConfig config, final String name) throws UiException
    {
        List <PdsPackageInfoVO> volist = null;
        int count = 0;
        try
        {
            volist = m_pdsSupplyPackageService.findPdsPackageInfoVOWithPageByName (config.getOffset (), config.getLimit (), name);
            count = m_pdsSupplyPackageService.findPdsPackageInfoVOCountByName (name);
        }
        catch (final Exception e)
        {
            PdsSupplyPackageServiceController.s_logger.error (e.getMessage ());
            throw new UiException ("查询失败", e);
        }
        final List <PdsPackageInfoModel> drugInfoModelList = ConvertUtils.convert (PdsPackageInfoModel.class, volist);
        return new BasePagingLoadResult <PdsPackageInfoModel> (drugInfoModelList, config.getOffset (), count);
    }

    @Override
    public void modifyPackageInfo (final List <PdsPackageInfoModel> modelList) throws UiException
    {
        PdsSupplyPackageServiceController.s_logger.debug ("进入modifyPackageInfo方法,入参:");
        if (modelList == null || modelList.isEmpty ())
        {
            return;
        }
        for (final PdsPackageInfoModel pdsPackageInfoModel : modelList)
        {
            if (pdsPackageInfoModel.getEnable () == null)
            {
                // 编辑
                try
                {
                    final PdsPackageInfoVO pdsPackageInfo = ConvertUtils.convert (PdsPackageInfoVO.class, pdsPackageInfoModel);
                    m_pdsSupplyPackageService.modifyPdsPackageInfoByExample (pdsPackageInfo);
                }
                catch (final Exception e)
                {
                    PdsSupplyPackageServiceController.s_logger.error (e.getMessage ());
                    throw new UiException (e.getMessage (), e);
                }
            }
            else
            {

                try
                {
                    // 新增
                    final PdsPackageInfoVO pdsPackageInfo = ConvertUtils.convert (PdsPackageInfoVO.class, pdsPackageInfoModel);
                    m_pdsSupplyPackageService.addPdsPackageInfo (pdsPackageInfo);
                }
                catch (final Exception e)
                {
                    PdsSupplyPackageServiceController.s_logger.error (e.getMessage ());
                    throw new UiException (e.getMessage (), e);
                }
            }
        }
    }

    public void setPdsSupplyPackageService (final PdsSupplyPackageService pdsSupplyPackageService)
    {

        m_pdsSupplyPackageService = pdsSupplyPackageService;
    }
}